package com.eds.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eds.dto.*;
import com.eds.exception.BusException;
import com.eds.pojo.BaseResult;
import com.eds.pojo.CodeEnum;
import com.eds.pojo.CourseTableDTO;
import com.eds.service.CourseService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName：CourseController
 * @Author: vvSmile
 * @Date: 2025/03/21
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@RestController
@RequestMapping("/courseController")
public class CourseController {
    @Autowired
    private CourseService courseService;

    //1.查询所有课程
    @GetMapping("/selectAllCourses")
    public BaseResult selectAllCourse(int currentPage, int pageSize) {
        //如果页码和当前页码为空则初值
        if (currentPage == 0) {
            currentPage = CodeEnum.DEFAULT_VALUE.getCode();
        } else if (pageSize == 0) {
            pageSize = CodeEnum.DEFAULT_PAGE_SIZE_VALUE.getCode();
        }
        PageUtils<Course> coursePage = courseService.selectAllCourseService(currentPage, pageSize);
        if (coursePage == null) {
            throw new BusException(CodeEnum.SELECT_ALL_COURSES_PAGE_ERROR.getCode(), CodeEnum.SELECT_ALL_COURSES_PAGE_ERROR.getMessage());
        }
        return BaseResult.ok("分页查询所有课程成功！", coursePage);
    }

    //2.根据名称分页查询所有课程
    @GetMapping("/selectCourseByName")
    public BaseResult selectCourseByName(int currentPage, int pageSize, String name) {
        Page<Course> coursePage = courseService.selectCourseByNameService(currentPage, pageSize, name);
        if (coursePage == null) {
            throw new BusException(CodeEnum.SELECT_ALL_COURSES_PAGE_ERROR.getCode(), CodeEnum.SELECT_ALL_COURSES_PAGE_ERROR.getMessage());
        }
        return BaseResult.ok("分页查询所有课程成功！", coursePage);
    }

    //3.根据老师Id分页查询老师创建的课程
    @GetMapping("/selectCourseByTeacherId")
    public BaseResult selectCourseByTeacherId(int currentPage, int pageSize, Long teacherId) {
        Page<Course> coursePage = courseService.selectCourseByTeacherIdService(currentPage, pageSize, teacherId);
        if (coursePage == null) {
            throw new BusException(CodeEnum.SELECT_ALL_COURSES_PAGE_ERROR.getCode(), CodeEnum.SELECT_ALL_COURSES_PAGE_ERROR.getMessage());
        }
        return BaseResult.ok("分页查询所有课程成功！", coursePage);
    }

    //4.根据课程Id查课程信息
    @GetMapping("/selectCourseByCourseId")
    public BaseResult selectCourseByCourseId(Long courseId) {
        Course course = courseService.selectCourseByCourseIdService(courseId);
        if (course == null) {
            throw new BusException(CodeEnum.SELECT_COURSE_INFO_BY_ID_ERROR.getCode(), CodeEnum.SELECT_COURSE_INFO_BY_ID_ERROR.getMessage());
        }
        return BaseResult.ok("根据课程Id查课程信息成功", course);
    }

    //5.根据Token查课程信息
    @PostMapping("/selectCourseByToken")
    public BaseResult selectCourseByToken(@RequestHeader("authorization") String token) {
        List<Course> course = courseService.selectCourseByToken(token);
        if (course == null) {
            throw new BusException(CodeEnum.GET_COURSE_INFO_BY_TOKEN.getCode(), CodeEnum.GET_COURSE_INFO_BY_TOKEN.getMessage());
        }
        return BaseResult.ok("通过token获取课程信息成功！", course);
    }

    //6.通过课程ID查询老师信息
    @GetMapping("/selectTeacherInfo")
    public BaseResult selectTeacherInfo(Long courseId) {
        User user = courseService.selectTeacherInfo(courseId);
        if (user == null) {
            throw new BusException(CodeEnum.SELECT_USER_INFO_BY_USERID_ERROR.getCode(), CodeEnum.SELECT_USER_INFO_BY_USERID_ERROR.getMessage());
        }
        return BaseResult.ok("通过UserId获取用户信息成功！", user);
    }


    //7.增加课程
    @PostMapping("/insertCourse")
    public BaseResult insertCourse(@RequestHeader("authorization") String token, @ModelAttribute CourCreateRequest courCreateRequest) throws Exception {
        BaseResult insertCourseReturn = courseService.insertCourseService(token, courCreateRequest);
        return insertCourseReturn;
    }

    //8.删除课程
    @DeleteMapping("/delCourse")
    public BaseResult delCourse(@RequestHeader("authorization") String token, long courseId) {
        boolean delCourse = courseService.delCourseService(token, courseId);
        if (!delCourse) {
            throw new BusException(CodeEnum.DELETE_COURSE_ERROR.getCode(), CodeEnum.DELETE_COURSE_ERROR.getMessage());
        }
        return BaseResult.ok("删除课程成功！", delCourse);
    }

    //9.更新图片
    @PostMapping("/updateCourseImg")
    public BaseResult updateCourseImg(@RequestHeader("authorization") String token, @RequestParam("courseId") Long courseId, @RequestParam("relationType") String relationType, @RequestParam("file") MultipartFile file) {
        String updateCourseTitleReturn = courseService.updateCourseImg(token, courseId, relationType, file);
        if (updateCourseTitleReturn.isEmpty()) {
            throw new BusException(CodeEnum.UPDATE_COURSE_IMG_ERROR.getCode(), CodeEnum.UPDATE_COURSE_IMG_ERROR.getMessage());
        }
        return BaseResult.ok("更新成功！", updateCourseTitleReturn);
    }


    //10.删除图片(在编辑课程内容时）
    @PostMapping("/delCourseImg")
    public BaseResult delCourseImg(@RequestHeader("authorization") String token, @RequestParam("courseId") Long courseId, @RequestParam("relationType") String relationType) {
        Boolean delCourseImgReturn = courseService.delCourseImg(token, courseId, relationType);
        if (!delCourseImgReturn) {
            throw new BusException(CodeEnum.DELETE_COURSE_IMAGE_ERROR.getCode(), CodeEnum.DELETE_COURSE_IMAGE_ERROR.getMessage());
        }
        return BaseResult.ok("删除成功！", delCourseImgReturn);
    }

    //11.修改课程
    @PostMapping("/updateCourse")
    public BaseResult updateCourse(@RequestHeader("authorization") String token, @RequestBody CourCreateRequest courCreateRequest) throws JsonProcessingException {
        BaseResult updateCourse = courseService.updateCourseService(token, courCreateRequest);
        return updateCourse;
    }

    //12.获取课程表信息
    @GetMapping("/getCourseTable")
    public BaseResult getCourseTable(Long courseId) {
        List<TeacherSchedule> getCourseTableReturn = courseService.getCourseTable(courseId);
        if (getCourseTableReturn == null) {
            return BaseResult.error(CodeEnum.GET_COURSE_TABLE_ERROR);
        }
        return BaseResult.ok("获取课程表信息成功！", getCourseTableReturn);
    }

    //预约，传入token和课程信息进行预约，并将信息保存至course_booking表中
    @PostMapping("/bookCourse")
    public BaseResult bookCourse(@RequestHeader("authorization") String token, @RequestBody AppointmentRequestDTO appointmentRequest) {
        BaseResult bookCourseReturn = courseService.bookCourse(token, appointmentRequest);
        return bookCourseReturn;
    }

    //13.根据时间Id查询课程信息
    @GetMapping("/selectCourseInfoByScheduleId")
    BaseResult selectCourseInfoByScheduleId(@RequestHeader("authorization") String token, Long scheduleId) {
        Course CourseInfo = courseService.selectCourseInfoByScheduleId(token, scheduleId);
        if (CourseInfo == null) {
            return BaseResult.error(CodeEnum.SELECT_COURSE_INFO_BY_SCHEDULE_ID_ERROR);
        }
        return BaseResult.ok("查询课程信息成功！", CourseInfo);
    }

    //14.取消预约
    @PostMapping("/cancelBookCourse")
    public BaseResult cancelBookCourse(@RequestHeader("authorization") String token, @RequestBody CancleRequestDTO cancleRequestDTO) {
        BaseResult cancelBookCourseReturn = courseService.cancelBookCourse(token, cancleRequestDTO);
        return BaseResult.ok("取消成功！", cancelBookCourseReturn);
    }


    //15.订单调用
    @PostMapping("/ordersSelectCourseByScheduleId")
    Course ordersSelectCourseByScheduleId(@RequestHeader("authorization") String token, @RequestParam("scheduleId") Long scheduleId) {
        Course CourseInfo = courseService.selectCourseInfoByScheduleId(token, scheduleId);
        return CourseInfo;
    }

    //16.查询用户参加的课程
    @GetMapping("/selectCourseByUserId")
    public BaseResult selectCourseByUserId(@RequestHeader("authorization") String token) {
        List<Course> courseList = courseService.selectCourseByUserId(token);
        if (courseList == null) {
            return BaseResult.error(CodeEnum.GET_COURSE_TABLE_ERROR);
        }
        return BaseResult.ok("查询课程信息成功！", courseList);
    }

    //17.更新预约状态
    @PostMapping("/updateBookingStatus")
    int updateBookingStatus(@RequestParam("scheduleId") Long scheduleId, @RequestParam("status") int status) {
        int updateBookingStatus = courseService.updateBookingStatus(scheduleId, status);
        return updateBookingStatus;
    }

    //18.根据CourseId，ScheduleId,token获取预约信息
    @GetMapping("/selectBookingByScheduleIdAndCourseId")
    public BaseResult selectBookingByScheduleIdAndCourseId(@RequestHeader("authorization") String token, @RequestParam("scheduleId") Long scheduleId, @RequestParam("courseId") Long courseId) {
        CourseBooking selectBookingByScheduleIdAndCourseId = courseService.selectBookingByScheduleIdAndCourseId(token, scheduleId, courseId);
        if (selectBookingByScheduleIdAndCourseId == null) {
            return BaseResult.error(CodeEnum.COURSE_BOOKING_NOT_EXIST);
        }
        return BaseResult.ok("查询成功！", selectBookingByScheduleIdAndCourseId);
    }

    //查询老师的课程
    @GetMapping("/selectTeacherCourse")
    public List<Course> selectCourseByTeacherIdAIUsed(Long teacherId) {
        List<Course> courseList = courseService.selectCourseByTeacherIdAIUsed(teacherId);
        return courseList;
    }


    // 获取热门课程
    @PostMapping("/getHotCourses")
    List<Course> getHotCourses(@RequestParam("amount") int amount) {
        List<Course> courses = courseService.getHotCourses(amount);
        return courses;
    }

    @GetMapping("/selectCourseByCourseIdReturnCourse")
    Course selectCourseByCourseIdReturnCourse(@RequestParam("courseId") Long courseId) {
        Course course = courseService.selectCourseByCourseIdService(courseId);
        return course;
    }

    // 获取所有课程
    @GetMapping("/selectAllCourseList")
    public List<CourseDesc> selectAllCourseList() {
        return courseService.selectAllCourseList();
    }

    //学生课程表
    @GetMapping("/selectStudentCourseTable")
    public BaseResult selectStudentCourseTable(@RequestHeader("authorization") String token) {
        List<CourseTableDTO> courseBookings = courseService.selectStudentCourseBookingByUserId(token);
        if (courseBookings == null) {
            return BaseResult.error(CodeEnum.COURSE_BOOKING_NOT_EXIST);
        }
        return BaseResult.ok("查询成功！", courseBookings);
    }

    @GetMapping("/deleteCourseByTeacherId")
    int deleteCourseByTeacherId(@RequestParam("userId") Long userId) {
        return courseService.deleteCourseByTeacherId(userId);
    }

    @GetMapping("/selectCourseByCourseIdForOrders")
    public Course selectCourseByCourseIdForOrders(@RequestParam("courseId") Long courseId) {
        return courseService.selectCourseByCourseIdService(courseId);
    }

    @GetMapping("/selectAllCoursesForBack")
    public BaseResult<PageUtils<BackCourseDTO>> selectAllCourseForBack(int currentPage, int pageSize, String courseName) {
        //如果页码和当前页码为空则初值
        if (currentPage == 0) {
            currentPage = CodeEnum.DEFAULT_VALUE.getCode();
        } else if (pageSize == 0) {
            pageSize = CodeEnum.DEFAULT_PAGE_SIZE_VALUE.getCode();
        }
        PageUtils<BackCourseDTO> coursePage = courseService.selectAllCourseServiceForBack(currentPage, pageSize, courseName);
        if (coursePage == null) {
            throw new BusException(CodeEnum.SELECT_ALL_COURSES_PAGE_ERROR.getCode(), CodeEnum.SELECT_ALL_COURSES_PAGE_ERROR.getMessage());
        }
        return BaseResult.ok("分页查询所有课程成功！", coursePage);
    }

    //编辑课程价格
    @PostMapping("/updateCoursePrice")
    public BaseResult updateCoursePrice(@RequestBody Course course) {
        BaseResult updateCoursePrice = courseService.updateCoursePrice(course);
        if (updateCoursePrice == null) {
            return BaseResult.error(CodeEnum.UPDATE_COURSE_PRICE_ERROR);
        }
        return BaseResult.ok("修改课程价格成功！", updateCoursePrice);
    }

    //修改状态
    @PostMapping("/updateCourseStatus")
    public BaseResult updateCourseStatus(@RequestParam("courseId") Long courseId, @RequestParam("status") int status) {
        Course course = new Course();
        course.setCourseId(courseId);
        course.setStatus(status);
        int updateCourseStatus = courseService.updateCourseStatus(course);
        if (updateCourseStatus <= 0) {
            return BaseResult.error(CodeEnum.UPDATE_COURSE_STATUS_ERROR);
        }
        return BaseResult.ok("修改课程价格成功！", updateCourseStatus);
    }

    //8.删除课程
    @DeleteMapping("/delCourseForBack")
    public BaseResult delCourseForBack(long courseId) {
        boolean delCourse = courseService.delCourseForBack(courseId);
        if (!delCourse) {
            throw new BusException(CodeEnum.DELETE_COURSE_ERROR.getCode(), CodeEnum.DELETE_COURSE_ERROR.getMessage());
        }
        return BaseResult.ok("删除课程成功！", delCourse);
    }

    @GetMapping("/getCourseBoard")
    public BaseResult getCourseBoard() {
        BackCourseBoardDTO courseBoard = courseService.getCourseBoard();
        if (courseBoard == null) {
            return BaseResult.error(CodeEnum.GET_USER_BOARD_ERROR);
        }
        return BaseResult.ok("获取用户面板成功！", courseBoard);
    }

}
